<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户号" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入商户号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
       <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="状态" size="small" :clearable="true">
            <el-option
              v-for="dict in dict.type.aipay_member_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
         <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['aipay:member:add']">新增</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5"></el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList">
      <el-table-column label="商户号" align="center" prop="id" />
      <!-- <el-table-column label="上级代理" align="center" prop="parentId" /> -->
      <el-table-column label="角色组" align="center" prop="roleName" />
      <el-table-column label="用户名" align="center" prop="username" />
      <el-table-column label="昵称" align="center" prop="nickname" />
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <!-- <el-table-column label="币种数量" align="center" prop="coinCount" /> -->
      <el-table-column label="地址数量" align="center" prop="addressCount" />
      <el-table-column label="API Key" align="center" prop="apiKey">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleViewAddress(scope.row.apiKey, 'API Key')">查看</el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column label="登录时间" align="center" prop="loginTime" />
      <el-table-column label="登录IP" align="center" prop="loginIp" /> -->
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.aipay_member_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="加入时间" align="center" prop="ctime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ctime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['aipay:member:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['aipay:member:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.limit"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="商户号" prop="id" v-show="form.id">
          <el-input v-model="form.id" placeholder="" :disabled="true"/>
        </el-form-item>
        <el-form-item label="角色组" prop="roleId">
          <el-select v-model="form.roleId" placeholder="角色组" size="small">
            <el-option
              v-for="(item, index) in roleList"
              :key="item.roleId"
              :label="item.roleName"
              :value="item.roleId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入昵称" />
        </el-form-item>
        <!-- <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item> -->
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <!-- <el-form-item label="ApiKey设置" prop="apiKey">
          <el-input v-model="form.publicKey" type="textarea" :rows="3" placeholder="请输入ApiKey" />
          <div><el-tag type="danger">修改此项会影响线上交易，慎重！！</el-tag></div>
        </el-form-item> -->
        <el-form-item label="请求IP" prop="allowIp">
          <el-input v-model="form.allowIp" type="textarea" :rows="3" placeholder="请输入请求IP" />
          <div><el-tag type="danger">多个用,号分割, 修改此项会影响线上交易，慎重！！</el-tag></div>
        </el-form-item>
        <el-form-item label="回调URL设置" prop="notifyUrl">
          <el-input v-model="form.notifyUrl" placeholder="请输入回调URL" />
          <div><el-tag type="danger">不填此项将无法通知系统充提到账, 修改此项会影响线上交易，慎重！！</el-tag></div>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="状态" size="small">
            <el-option
              v-for="dict in dict.type.aipay_member_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="30%">
      <span>{{ dialogValue }}</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listMember, getMember, addMember, updateMember, delMember  } from "@/api/aipay/member";
import { optionselectList } from "@/api/aipay/role";

export default {
  name: "Resource",
  dicts: ['sys_bool_status', 'aipay_settle_type', 'aipay_member_status', 'aipay_proxy_profit_status', 'aiapy_withdraw_fee_type', 'sys_open_close'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //表格数据
      postList: [],
      roleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      activeName: "public",
      dialogVisible: false,
      dialogTitle: '',
      dialogValue: '',
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        status: undefined,
        id: undefined,
        username: undefined,
        mobile: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        nickname: [
          { required: true, message: "昵称不能为空", trigger: "blur" }
        ],
        password: [],
        payPassword: [],
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        email: [
          { required: true, message: "邮箱不能为空", trigger: "blur" }
        ],
        roleId: [
          { required: true, message: "角色组不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getRoleList();
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listMember(this.queryParams).then(response => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getRoleList(){
      optionselectList().then(response => {
        this.roleList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    handleViewAddress(row, title){
      this.dialogTitle = title
      this.dialogValue = row
      this.dialogVisible = true
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getMember(row.id).then(response => {
        this.form = response.data;
        this.form.settleType = this.form.settleType + '';
        this.form.withdrawFeeType = this.form.withdrawFeeType + '';
        this.form.proxyProfitType = this.form.proxyProfitType + '';
        this.form.autoPayStatus = this.form.autoPayStatus + '';
        this.form.batchPayStatus = this.form.batchPayStatus + '';
        this.form.payApiStatus = this.form.payApiStatus + '';
        this.form.status = this.form.status + '';
        this.form.password = undefined;
        this.form.payPassword = undefined;
        this.open = true;
        this.title = "修改商户";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateMember(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMember(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMember(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    }
    
  }
};
</script>
